package com.globallogic.strelchuk.usermanager.bean;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.context.annotation.Scope;

import com.globallogic.strelchuk.usermanager.domain.User;
import com.globallogic.strelchuk.usermanager.service.UserService;

@Named
@Scope("session")
public class UserBean implements Serializable {

	private static final long serialVersionUID = 1L;

	@Inject
	private UserService userService;

	private Integer id;
	private String login;
	private String password;
	private String name;
	private String email;
	private String role;
	private String status;
	private Date createdate;

	@SuppressWarnings("unused")
	private List<User> listAllUsers;

	private User currentUser;

	public UserBean() {
		currentUser = new User();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getCreatedate() {
		return createdate;
	}

	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public List<User> getListAllUsers() {
		return userService.listAllUsers();
	}

	public void setListAllUsers(List<User> listAllUsers) {
		this.listAllUsers = listAllUsers;
	}

	public User getCurrentUser() {
		return currentUser;
	}

	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String loginCheck() {
		User usr = new User();
		usr.setLogin(getLogin());
		usr.setPassword(getPassword());

		User usrLogin = userService.getUserByLogin(getLogin());

		if (userService.isCorrectPassword(usr)) {
			setCurrentUser(usrLogin);
			clearForm();
			return "activities";
		}
		return null;
	}

	public String saveAction(User user) {
		userService.editUser(user, user.getId());
		clearForm();
		return "userlist";
	}

	public String editAction(User user) {
		setCurrentUser(user);
		return "edituser";
	}

	public String addAction() {
		User usr = new User();
		usr.setLogin(getLogin());
		usr.setName(getName());
		usr.setPassword(getPassword());
		usr.setEmail(getEmail());
		usr.setRole(getRole());
		usr.setStatus(getStatus());

		userService.addAUser(usr);
		clearForm();
		return null;
	}

	public String deleteAction(Integer id) {
		userService.removeUser(id);
		return null;
	}

	private void clearForm() {
		setLogin("");
		setPassword("");
		setName("");
		setEmail("");
		setCreatedate(null);
		setRole(null);
		setStatus(null);
	}
}